using System;
using System.Collections;
using System.Collections.Generic;
using Resoure.Script.Utilities;
using UnityEngine;
using UnityEngine.Events;

public class Playercharacter : MonoBehaviour
{
    public float maxHp;
    public float currenthHp;

    public float maxElectricity;
    public float currentElectricity;

    public GameObject flashLight;
    public bool isUseFlashLight;
    [Header("物品1")]
    public int item1Count;
    public float item1MaxCD;
    private MyTimer item1Timer;
    [Header("物品2")]
    public int item2Count;
    public float item2MaxCD;
    private MyTimer item2Timer;
    [Header("物品3")]
    public int item3Count;
    public float item3MaxCD;
    private MyTimer item3Timer;
    [Header("物品4")]
    public int item4Count;
    public float item4MaxCD;
    private MyTimer item4Timer;
    
    [Header("关联性依赖")]
    public ProjectWinter WinterProject;
    public UnityEvent<int,int> GetItemEvent;
    public UnityEvent<int,int> UseItemEvent;
    public UnityEvent<int, float> CoolDownItemEvent;
    public UnityEvent<float> OnElectrictyChange;

    public void Start()
    {
        currenthHp = maxHp;
        currentElectricity = maxElectricity;
        
        item1Timer = new MyTimer(item1MaxCD);
        item2Timer = new MyTimer(item2MaxCD);
        item3Timer = new MyTimer(item3MaxCD);
        item4Timer = new MyTimer(item4MaxCD);
       
    }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) // 按空格键
        {
            GetItem(1);
        }

        CoolDownItem();
        
        ChangeElectricity();
        CanUseFlashLight();

    }

    private void ChangeElectricity()
    {
        if (isUseFlashLight)
        {
            currentElectricity -= Time.deltaTime * WinterProject.electricityDeclineFactor;
            OnElectrictyChange?.Invoke(currentElectricity/maxElectricity);
        }

        if (currentElectricity <= 0)
        {
            isUseFlashLight = false;
        }
    }

    private void CanUseFlashLight()
    {
        if (currentElectricity < 0)
        {
            flashLight.SetActive(false);
        }
        else
        {
            flashLight.SetActive(true);
        }
    }


    private void ChangeHp()
    {
        currenthHp -= (Time.deltaTime * WinterProject.hpDeclineFactor);
    }

    public void RecoveryHp()
    {
        if (currenthHp <= maxHp - 10)
        {
            currenthHp += 10f * WinterProject.hpRegenFactor;
        }
    }

    public void GetItem(int i)
    {
        switch (i)
        {
            case 1:item1Count++;GetItemEvent?.Invoke(1,item1Count);break;
            case 2:item2Count++;GetItemEvent?.Invoke(2,item2Count);break;
            case 3:item3Count++;GetItemEvent?.Invoke(3,item3Count);break;
            case 4:item4Count++;GetItemEvent?.Invoke(4,item4Count);break;
            
        }
    }

    public void UseItem(int i)
    {
        switch (i)
        {
            case 1:
                if (item1Timer.CurrentTime <= 0&& item1Count>0)
                {
                    item1Count -=1;
                    item1Timer.resetTimer();
                    UseItemEvent?.Invoke(1,item1Count);
                }
                else
                {
                    Debug.Log("冷却中");
                }
                break;
            
            case 2:
                if (item2Timer.CurrentTime <= 0&& item2Count>0)
                {
                    item2Count -=1;
                    item2Timer.resetTimer();
                    UseItemEvent?.Invoke(2,item2Count);
                }    
                else
                {
                    Debug.Log("冷却中");
                }break;
            
            case 3:
                if (item3Timer.CurrentTime <= 0&& item3Count>0)
                {
                    item2Count -=1;
                    item3Timer.resetTimer();
                    UseItemEvent?.Invoke(3,item3Count);
                }
                else
                {
                    Debug.Log("冷却中");
                }break;
            case 4:
                if (item4Timer.CurrentTime <= 0&& item4Count>0)
                {
                    item4Count -=1;
                    item4Timer.resetTimer();
                    UseItemEvent?.Invoke(4,item4Count);
                }
                else
                {
                    Debug.Log("冷却中");
                }break;
            default: break;
        }
    }

    public void CoolDownItem()
    {
        if (item1Timer.CurrentTime > 0)
        {
            item1Timer.updateMyTimer(1);
            CoolDownItemEvent?.Invoke(1,item1Timer.CurrentTime/item1Timer.MaxTime);
        }

        if (item2Timer.CurrentTime > 0)
        {
            item2Timer.updateMyTimer(1);
            CoolDownItemEvent?.Invoke(2, item2Timer.CurrentTime / item2Timer.MaxTime);
        }

        if (item3Timer.CurrentTime > 0)
        {
            item3Timer.updateMyTimer(1);
            CoolDownItemEvent?.Invoke(3, item3Timer.CurrentTime / item3Timer.MaxTime);
        }

        if (item4Timer.CurrentTime > 0)
        {
            item4Timer.updateMyTimer(1);
            CoolDownItemEvent?.Invoke(4, item4Timer.CurrentTime / item4Timer.MaxTime);
        }

    }

}
